package com.levi.utils.log;

import android.util.Log;

import com.levi.utils.log.printer.Printer;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;

/**
 * Created by Levi on 2018/3/13.
 */
public final class Logger {

    private static final List<Printer> PRINTERS = new ArrayList<>();

    private Logger() {
        throw new AssertionError("Don't instance!");
    }

    public static void addPrinter(@NonNull Printer printer) {
        PRINTERS.add(printer);
    }

    public static void i(String tag, String msg, Object... args) {
        log(Log.INFO, tag, null, msg, args);
    }

    public static void i(String tag, String msg, Throwable throwable) {
        log(Log.INFO, tag, throwable, msg);
    }

    public static void i(String tag, Throwable throwable) {
        log(Log.INFO, tag, throwable, null);
    }

    public static void i(String tag, Throwable throwable, String msg, Object... args) {
        log(Log.INFO, tag, throwable, msg, args);
    }

    public static void d(String tag, String msg, Object... args) {
        log(Log.DEBUG, tag, null, msg, args);
    }

    public static void d(String tag, String msg, Throwable throwable) {
        log(Log.DEBUG, tag, throwable, msg);
    }

    public static void d(String tag, Throwable throwable) {
        log(Log.DEBUG, tag, throwable, null);
    }

    public static void d(String tag, Throwable throwable, String msg, Object... args) {
        log(Log.DEBUG, tag, throwable, msg, args);
    }

    public static void e(String tag, String msg, Object... args) {
        log(Log.ERROR, tag, null, msg, args);
    }

    public static void e(String tag, String msg, Throwable throwable) {
        log(Log.ERROR, tag, throwable, msg);
    }

    public static void e(String tag, Throwable throwable) {
        log(Log.ERROR, tag, throwable, null);
    }

    public static void e(String tag, Throwable throwable, String msg, Object... args) {
        log(Log.ERROR, tag, throwable, msg, args);
    }

    public static void v(String tag, Throwable throwable, String msg, Object... args) {
        log(Log.VERBOSE, tag, throwable, msg, args);
    }

    public static void w(String tag, Throwable throwable, String msg, Object... args) {
        log(Log.WARN, tag, throwable, msg, args);
    }

    public static void wtf(String tag, Throwable throwable, String msg, Object... args) {
        log(Log.ASSERT, tag, throwable, msg, args);
    }

    public static void log(int priority, String tag, Throwable throwable, String msg, Object... args) {
        for (Printer printer : PRINTERS) {
            printer.log(priority, tag, createMessage(msg, args), throwable);
        }
    }

    private static String createMessage(String msg, Object... args) {
        return msg == null || args == null || args.length == 0 ? msg : String.format(msg, args);
    }
}
